// pages/search/search.js
import api from '../../utils/api.js';
import network from '../../utils/network.js';
import formatDate from '../../utils/format_date';

const HISTORY = 'history';
let pageIndex = 1;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        history: [],
        list: [],
        name: '',
        isRefresh: false,
        searched: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let history = wx.getStorageSync(HISTORY);
        if (history) {
            this.setData({
                history: JSON.parse(history)
            })
        }

        this.hasData = false;
    },

    onPullDownRefresh() {
        this.onRefresh();
    },

    onRefresh() {
        this.setData({
            isRefresh: true
        })
        pageIndex = 1;
        this.request();
    },

    onReachBottom() {
        if (this.hasData) {
            this.request();
        }
    },

    /**
     * 请求数据 
     * 将每一页的数据放一个数组，即形成一个二维数组,减少页面渲染的次数
     */
    request: function () {
        let param = {
            pageNo: pageIndex,
            pageSize: 20,
            onShelves: 1,
            queryType: 1,
            name: this.data.name
        }

        network.getJson(api.activePage, param).then(res => {
            this.setData({
                isRefresh: false,
                searched: true
            })
            if (pageIndex == 1) {
                this.setData({
                    list: []
                })
            }
            let list = res.data.list;
            for (let i = 0; i < list.length; i++) {
                list[i].images = list[i].images.split(',');
                let time = list[i].bookingTimes;
                let len = time.length;
                if (len > 0) {
                    list[i].activeDate = formatDate.formatWeek4(time[0].startTime, time[len - 1].endTime);
                }

                if (list[i].users.length > 5) {
                    list[i].users = list[i].users.slice(-5);
                }
            }
            this.setData({
                history: this.data.history,
                list: this.data.list.concat(list)
            })
            if (this.data.list.length < res.data.total) {
                this.hasData = true;
                pageIndex += 1;
            } else {
                this.hasData = false;
            }
        })
    },

    onSubmitValue(e) {
        if (e.detail.value) {
            this.data.name = e.detail.value;
            this.onRefresh();
            this.data.history.unshift(e.detail.value);
            if (this.data.history.length > 20) {
                this.data.history.splice(this.data.history.length - 1);
            }
            wx.setStorageSync(HISTORY, JSON.stringify(this.data.history));
        }
    },

    delTap() {
        wx.removeStorage({
            key: HISTORY
        })
        this.setData({
            history: []
        })
    },

    onSearch(e) {
        let index = e.currentTarget.dataset.index;
        this.setData({
            name: this.data.history[index]
        })
        this.onRefresh();
    },

    cancelTap() {
        // this.setData({
        //     history: this.data.history,
        //     list: [],
        //     key: ''
        // })
        wx.navigateBack();
    },

    goActiveDetails(e) {
        wx.navigateTo({
            url: '/active/active-details/active-details?id=' + e.currentTarget.dataset.id
        })
    },
})